<script setup>
import TopButtonCenter from "@/components/Pengyq/TopButtonCenter.vue";
import { updateInfoPlAPI, addShopSumAPI } from "@/server/api/rechargeOrder";
import { payOrderAPI } from "@/server/api/itemOrders";
import { payShopOrderAPI } from "@/server/api/shopOrder";
import { showSuccessToast, showFailToast } from "vant";
import { ItemShopStore } from "@/stores/ItemAnShop";
import { useUseStore } from "@/stores/user";
import { getInfoAPI } from "@/server/api/userInfo";
import { useRoute } from "vue-router";
import { useRouter } from "vue-router";
import { onMounted, ref } from "vue";
import { getJWTFun } from "@/hook/jsrsasign";
import { ToastFun } from "@/hook/toast";
import { debounce } from "@/utils/debounce"
import { payLiveOrderApi } from "@/server/api/profile";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
/*
&注册区
*/
const userStore = useUseStore();
const userInfome = ref({}); //定义用户余额数据
const route = useRoute();
const router = useRouter();
const ItemShop = ItemShopStore();

/*
&函数区域
*/
//定义下单接口函数
const payOrder = async (itmePayReq) => {
  try {
    console.log(itmePayReq);
    let token = getJWTFun(itmePayReq);

    ToastFun("Loading", { duration: 0 });
    const res = await payOrderAPI(token);

    console.log(res)
    if (res.data.code === 200) {
      // showSuccessToast($t("pay.success"));
      ToastFun("show", { msg: res.data.data });
      router.push({ path: "Successful", query: { price: route.query.price } });
    } else {
      console.log("支付失败");
      ToastFun("fail", { msg: t("item.details.NetworkError") });
    }
    console.log(res);
  } catch (error) {
    ToastFun("fail", { msg: t("item.details.NetworkError") });
  }
};

//定义shop下单接口函数
const payShopOrder = async (Trading) => {
  console.log(Trading)
  ToastFun("Loading", { msg: "" });

  let token = getJWTFun(Trading);

  // const res = await payShopOrderAPI(token); //实体店商品 
  const res = await payLiveOrderApi(token)


  if (res.data.code == 200) {
    ToastFun("", {});
    router.push({ path: "Successful", query: { price: route.query.price, data: res.data.msg } });
  } else {
    console.log("支付失败");
    ToastFun("", {});
  }
  console.log(res);
};

// 获取用户余额数据
const getInfo = async () => {
  const res = await getInfoAPI(userStore.userId);
  userInfome.value = res.data.data;
  console.log(res);
};

// 点击付款函数u
const pay = () => {
  if (ItemShop.IdtStatus == "item") {
    const itmePayReq = {
      uid: userStore.userId,
      orderIdArray: [route.query.orderId],
      payPointSum: route.query.price.toString(),
    };

    // let data = {
    //   commodityId: route.query.commodityId,
    //   number: Number(route.query.number),
    //   orderId: route.query.orderId,
    //   transactionAmount: route.query.price,
    //   uid: userStore.userId,
    // };

    userInfome.value.pointBalance > route.query.price
      ? payOrder(itmePayReq)
      : ToastFun("", {});;
  } else {
    // let data = {
    //   commodityId: route.query.commodityId,
    //   number: Number(route.query.number),
    //   orderId: route.query.orderId,
    //   transactionAmount: route.query.price,
    //   uid: userStore.userId,
    // };

    const Trading = {
      commodityId: route.query.commodityId,
      number: Number(route.query.number),
      orderId: route.query.orderId,
      transactionAmount: route.query.price,
      uid: userStore.userId,
    }

    userInfome.value.pointBalance > route.query.price
      ? payShopOrder(Trading)
      : ToastFun("", {});;
  }
};


const debouncePayFun = debounce(pay, 1000);

onMounted(() => {
  // console.log(route.query,"11111")
  getInfo();
});
</script>

<template>
  <!-- 主体模块 -->
  <main class="payOrders">
    <top-button-center title="Pay for order" style="background-color: transparent" />

    <!-- 个人模块 -->
    <div class="price">
      <h5><b> Pi/eCNY</b> {{ route.query.price }}</h5>
    </div>

    <div class="card">
      <h5>Increase the Number of Listings</h5>
      <div class="bottom">
        <div class="tex">{{ $t("pay.from") }}</div>
        <div class="addCart">
          <div class="total">
            Room 501,Unit 6,Building 20 North Donghuashi Residential BeiJing
            City
          </div>
        </div>
      </div>
    </div>

    <footer>
      <div class="pay" @click="debouncePayFun">{{ $t("pay.title") }}</div>
    </footer>
  </main>
</template>

<style lang="scss" scoped>
.payOrders {
  font-size: 14rem;
  position: relative;
  background-color: #f5f5f5;
  height: 100vh;

  .card {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    padding: 10rem;
    margin-top: 20rem;
    width: 100%;
    height: 140rem;
    background-color: #fff;
    border-radius: 10rem;

    h5 {
      font-size: 20rem;
      font-weight: 500;
    }

    .bottom {
      display: flex;
      justify-content: space-between;
      margin-top: auto;

      .tex {
        width: 40rem;
        text-align: center;
      }

      .addCart {
        padding-left: 20rem;
        color: #ccc;
        margin-left: auto;

        .total {
          b {
            font-size: 16rem;
          }
        }
      }
    }

    .title {
      font-size: 18rem;
      display: flex;
      justify-content: space-between;
    }
  }

  .price {
    padding: 50rem 0;

    h5 {
      text-align: center;
      font-size: 40rem;
      color: #642e8e;

      b {
        font-size: 16rem;
      }
    }
  }
}

footer {
  .pay {
    position: absolute;
    bottom: 30rem;
    left: 10rem;
    text-align: center;
    width: 90%;
    height: 40rem;
    line-height: 40rem;
    color: #fff;
    background-color: #642e8e;
    border-radius: 5rem;
    font-size: 16rem;
  }
}
</style>
